package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.Utilities;

import dao.Course;
import dao.CourseDAO;

public class CourseManageServlet extends HttpServlet {

    /**
     * The doGet method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {

	this.doPost(request, response);
    }

    /**
     * The doPost method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to
     * post.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {

	if (request.getQueryString() != null) {
	    String action = request.getParameter("action");
	    CourseDAO courseService = new CourseDAO();
	    String courseDescribe = request.getParameter("describe1").trim();
	    String chapterCountsString = request.getParameter("counts").trim();
	    int chapterCounts = Integer.parseInt(chapterCountsString);
	    if ("add".equals(action)) {
		String courseName = request.getParameter("courseName").trim();
		String gradeString = request.getParameter("grade").trim();
		if (courseName.length() == 2 && gradeString.length() == 2) {
		    StringBuffer buffer = new StringBuffer(gradeString);
		    buffer.append(courseName);
		    Course course = new Course();
		    course.setCourseid(buffer.toString());
		    buffer = null;
		    buffer = new StringBuffer(
			    Utilities.getGradeName(gradeString));
		    buffer.append(Utilities.getCourseName(courseName));
		    course.setName(buffer.toString());
		    course.setDescription(courseDescribe);
		    course.setChaptercounts(chapterCounts);
		    if (courseService.save(course)) {
			request.setAttribute("success", "true");
		    } else {
			request.setAttribute("success", "false");
		    }
		    request.getRequestDispatcher("../admin/addCourse.jsp")
			    .forward(request, response);
		}
	    } else if ("modify".equals(action)) {
		String courseId = request.getParameter("courseId").trim();
		String courseName = request.getParameter("courseName").trim();
		Course course = new Course();
		course.setCourseid(courseId);
		course.setName(courseName);
		course.setDescription(courseDescribe);
		course.setChaptercounts(chapterCounts);
		if (courseService.update(course)) {
		    request.setAttribute("success", "true");
		} else {
		    request.setAttribute("success", "false");
		}
		request.getRequestDispatcher("../gradehead/modifyCourse.jsp")
			.forward(request, response);
	    }
	}
    }

}
